package edu.cs.networkmonitor.application;

import java.util.List;
import java.util.Vector;

import edu.cs.networkmonitor.devices.BaseSource;
import edu.cs.networkmonitor.devices.DeviceSource;
import edu.cs.networkmonitor.devices.IPSource;
import edu.cs.networkmonitor.devices.PortSource;
import edu.cs.networkmonitor.scanner.*;
import edu.cs.networkmonitor.scheduler.NetTaskScheduler;
import edu.cs.networkmonitor.utils.NetUtils;

public class NetMonitorDriver {

	public enum SCANTYPE {
		DEVICE, IP, PORT
	}

	private DeviceScanner _dScanner;
	private IPScanner _ipScanner;
	private PortScanner _pScanner;
	private NetTaskScheduler scheduler;

	public NetMonitorDriver() {
		scheduler = new NetTaskScheduler();
	}

	public void scan(SCANTYPE type, String low, String high) {
		scan(type, low, high, null);
	}
	public void scan(SCANTYPE type, String low, String high, String host) {
		switch (type) {
		case PORT:
			try {
			_pScanner = new PortScanner(host, Integer.parseInt(low), Integer.parseInt(high));
			_pScanner.scan();
			} catch (NumberFormatException e){
				System.out.println("Invalid ports values");
			}
			break;
		case IP:
			_ipScanner = new IPScanner(low, high);
			_ipScanner.scan();
			break;
		case DEVICE:
			_dScanner = new DeviceScanner();
			_dScanner.scan();
			break;
		default:
			break;
		}
	}
	
	public boolean scheduleDevice(BaseSource s) {
		return scheduler.addSource(s);
	}
	
	public void startMonitor(BaseSource s, MonitorBoard monitorInterface) {
		scheduler.scheduleTask(s, monitorInterface);
	}

	public void stopMonitor(BaseSource s) {
		scheduler.stopScheduledTask(s);
	}
	
	public void removeMonitor(BaseSource s) {
		scheduler.removeScheduledTask(s);
	}
	
    public Object[][] getColumnData(SCANTYPE type) {
        switch (type) {
        case PORT:
            return _pScanner.getColumnData();
        case IP:
            return _ipScanner.getColumnData();
        case DEVICE:
            return _dScanner.getColumnData();
        default:
            return new Object[0][0];
        }
    }

    public List<DeviceSource> getDevices() {
        return _dScanner == null ? null : _dScanner.getSources();
    }
    
    public Vector<IPSource> getIPS() {
    	return _ipScanner == null ? null : _ipScanner.getSources();
    }
    
    public Vector<PortSource> getPorts() {
    	return _pScanner == null ? null : _pScanner.getPorts();
    }

	public Object[][] getScheduledData() {
		Object[][] data = new Object[scheduler.getSources().size()][3];
		for(int i=0;i< scheduler.getSources().size(); i++){
			BaseSource s= scheduler.getSources().get(i);
			 data[i][0] = s.getSourceName();
			// data[i][1] = s.getDeviceDescription();
			 data[i][1] = s.getEmailList() != null ? NetUtils.flattenArray(s.getEmailList()) : "";
			 data[i][2] = s.getTime();
		}
		return data;
	}

	public List<BaseSource> getSources() {
		return scheduler.getSources();
	}
	
	public boolean isSourceRunning(BaseSource s) {
		return scheduler.isSourceRunning(s); 
	}
}
